package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.model.Book;
import com.zhiyou.service.BookService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("Book/")
public class BookController {

	@Autowired
	BookService bookService;
	/**
	 * 添加图书
	 * @param book
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("add")
	public JsonResult add(Book book,HttpServletRequest request,HttpServletResponse response){
		try {
			bookService.add(book);
			return new JsonResult("插入书本成功");
		} catch (Exception e) {
			return new JsonResult(500,"插入失败");
		}
	}
	/**
	 * 更新图书信息
	 * @param book
	 * @param request
	 * @param response
	 * @return
	 */
	@PutMapping("update")
	public JsonResult update(Book book,HttpServletRequest request,HttpServletResponse response){
		try {
			bookService.update(book);;
			return new JsonResult("修改书本成功");
		} catch (Exception e) {
			return new JsonResult(500,"修改失败");
		}
	}
	/**
	 * 根据图书id删除图书
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id")int id,HttpServletRequest request,HttpServletResponse response){
		try {
			bookService.delete(id);
			return new JsonResult("删除书本成功");
		} catch (Exception e) {
			return new JsonResult(500,"删除失败");
		}
	}
	/**
	 * 根据图书id批量删除图书
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("delete")
	public JsonResult deleteAll(int[] ids,HttpServletRequest request,HttpServletResponse response){
		try {
			bookService.deleteAll(ids);
			return new JsonResult("批量删除书本成功");
		} catch (Exception e) {
			return new JsonResult(500,"删除失败");
		}
	}
	/**
	 * 批量删除图书
	 */
	
	/**
	 * 根据图书id查询图书详细信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("{id}")
	public JsonResult selectById(@PathVariable("id")int id,HttpServletRequest request,HttpServletResponse response){
		try {
			Book selectById = bookService.selectById(id);
			return new JsonResult(selectById,"查询书籍详细信息成功");
		} catch (Exception e) {
			return new JsonResult(500,"查询失败");
		}
	}
	/**
	 * 根据科目id和类别id查询所有图书
	 * @param id
	 * @param type
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list/{id}/{type}")
	public JsonResult list(@PathVariable("id")int id,@PathVariable("type")int type,HttpServletRequest request,HttpServletResponse response){
		try {
			List<Book> list = bookService.selectAll(id, type);
			return new JsonResult(list,"根据科目和类型查询书籍成功");
		} catch (Exception e) {
			return new JsonResult(500,"查询失败");
		}
	}
	/**
	 * 根据图书姓名进行模糊查询
	 * @param name
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("list")
	public JsonResult list(String name,HttpServletRequest request,HttpServletResponse response){
		try {
			List<Book> list = bookService.selectByName(name);
			return new JsonResult(list,"根据名字模糊查询书籍成功");
		} catch (Exception e) {
			return new JsonResult(500,"查询失败");
		}
	}
}
